﻿/**
* QSmart Andon
*
* 2012-10-10
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.SystemManage
{
    public class RoleService : DbHelperBase
    {
        public List<RoleEntity> GetRoleList()
        {
            var list = new List<RoleEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append("SELECT RoleID,RoleName FROM UT_Role");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new RoleEntity();
                            if (reader["RoleID"] != DBNull.Value)
                                model.RoleID = new Guid(reader["RoleID"].ToString());
                            model.RoleName = reader["RoleName"] == obj ? null : Convert.ToString(reader["RoleName"]);
                            list.Add(model);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        /// <summary>
        /// 获取角色分页
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public List<RoleEntity> GetRoleList(RoleQcEntity qcmodel)
        {
            var list = new List<RoleEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by InputTime) AS idx,RoleID,RoleName,OpRight,SignRight,FixRight,Remark FROM UT_Role ");
            strSql.Append(" WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.RoleName))
            {
                strSql.Append(" AND RoleName like '%" + qcmodel.RoleName + "%'");
            }
            strSql.Append(") AS T ");

            strSql.Append(" Where T.idx between " + start + " and " + end);

            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }

            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new RoleEntity();
                            if (reader["RoleID"] != DBNull.Value)
                                model.RoleID = new Guid(reader["RoleID"].ToString());
                            model.RoleName = reader["RoleName"] == obj ? null : Convert.ToString(reader["RoleName"]);
                            if (reader["OpRight"] != DBNull.Value)
                                model.OpRight = Convert.ToInt32(reader["OpRight"].ToString());
                            if (reader["SignRight"] != DBNull.Value)
                                model.SignRight = Convert.ToInt32(reader["SignRight"].ToString());
                            if (reader["FixRight"] != DBNull.Value)
                                model.FixRight = Convert.ToInt32(reader["FixRight"].ToString());
                            model.Remark = reader["Remark"] == obj ? null : Convert.ToString(reader["Remark"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool Remove(string roleId)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_Role Where RoleID=@RoleID ");
            var result = false;
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RoleID", roleId);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public int GetRoleCount(RoleQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_Role AS A WHERE 1=1 ");

            if (!string.IsNullOrEmpty(qcmodel.RoleName))
            {
                strSql.Append(" AND RoleName like '%" + qcmodel.RoleName + "%'");
            }

            var count = 0;

            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        /// <summary>
        /// 获取角色单条信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public RoleEntity GetRoleInfo(string roleId)
        {
            var model = new RoleEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT RoleID, RoleName,OpRight,SignRight,FixRight,Remark FROM UT_Role Where RoleID=@RoleID ");
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
               
                try
                {
                    Db.AddInParameter(cmd, "@RoleID", roleId);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["RoleID"] != DBNull.Value)
                                model.RoleID = new Guid(reader["RoleID"].ToString());
                            model.RoleName = reader["RoleName"] == obj ? null : Convert.ToString(reader["RoleName"]);
                            if (reader["OpRight"] != DBNull.Value)
                                model.OpRight = Convert.ToInt32(reader["OpRight"].ToString());
                            if (reader["SignRight"] != DBNull.Value)
                                model.SignRight = Convert.ToInt32(reader["SignRight"].ToString());
                            if (reader["FixRight"] != DBNull.Value)
                                model.FixRight = Convert.ToInt32(reader["FixRight"].ToString());
                            model.Remark = reader["Remark"] == obj ? null : Convert.ToString(reader["Remark"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddRole(RoleEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO UT_Role(RoleID,RoleName,OpRight,SignRight,FixRight,Remark) ");
            strSql.Append(" VALUES(@RoleID,@RoleName,@OpRight,@SignRight,@FixRight,@Remark) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RoleID", model.RoleID);
                    Db.AddInParameter(cmd, "@RoleName", model.RoleName);
                    Db.AddInParameter(cmd, "@OpRight", model.OpRight);
                    Db.AddInParameter(cmd, "@SignRight", model.SignRight);
                    Db.AddInParameter(cmd, "@FixRight", model.FixRight);
                    Db.AddInParameter(cmd, "@Remark", model.Remark);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="model"></param>
        public bool ModifyRole(RoleEntity model)
        {
            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_Role SET [RoleName] = @RoleName,[OpRight]=@OpRight,[SignRight]=@SignRight,[FixRight]=@FixRight,[Remark] = @Remark WHERE RoleID = @RoleID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RoleName", model.RoleName);
                    Db.AddInParameter(cmd, "@OpRight", model.OpRight);
                    Db.AddInParameter(cmd, "@SignRight", model.SignRight);
                    Db.AddInParameter(cmd, "@FixRight", model.FixRight);
                    Db.AddInParameter(cmd, "@Remark", model.Remark);
                    Db.AddInParameter(cmd, "@RoleID", model.RoleID);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public int GetRoleCount(string id)
        {
            var strSql = new StringBuilder();
            strSql.Append(" SELECT COUNT(*) AS Total FROM UT_Role AS A INNER JOIN UT_User AS B ON A.RoleID=B.RoleID WHERE A.RoleID=@RoleID ");
            var result = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RoleID", id);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            result = Convert.ToInt32(reader["Total"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return result;
            }
        }
    }
}
